<?php

	// Lifeline 1.0
	// Written & designed by David Correll with some help from Travis Sinnott, Adam Berlin and the interwebs.
	// Hat-tips to Jeremy Zilar for the inspiration: http://silencematters.com/2007/08/20/living-in-the-11th-hour/
	// Licensed under a GNU GPL v3 License 
	// Tested in Firefox 2 (Mac & Win), Safari 3, IE6, IE7 & Opera 9 (Mac). Will hopefully work in others as well.

	// Make sure the form was submitted
	if (!$_REQUEST['y']) {
		Header( "Location: ." );
	}

	// variable construction
	// -------------------------------------------------------------------------------------
	
	$name = $_REQUEST['fn'] . " " . $_REQUEST['ln'];
	
	$dob_month = $_REQUEST['m'];
	$dob_day = $_REQUEST['d'];
	$dob_year = $_REQUEST['y'];

	$birth = $dob_month . "/" . $dob_day . "/" . $dob_year;

	$subpath = substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'], "/"));
	$relativePath = $subpath . "/";
	$uri = $relativePath.$_REQUEST['ln']."/".$_REQUEST['fn']."/".$dob_month.$dob_day.$dob_year;
	$link = "http://".$_SERVER['HTTP_HOST'].$uri;
	
	if ($_SERVER['REQUEST_METHOD']=="POST") {
		header("Location: $uri");
	} 
	
	
	// Here's where the magic happens
	// -------------------------------------------------------------------------------------
	function dateDiff($dformat, $endDate, $beginDate) {
		$date_parts1=explode($dformat, $beginDate);
		$date_parts2=explode($dformat, $endDate);
		$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
		$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
		return $end_date - $start_date;
	}
	
	function deathDate($estamitedLife, $birthdate) {
		$birthDateParts =  explode("/", $birthdate);      
		$startDate = gregoriantojd($birthDateParts[0], $birthDateParts[1], $birthDateParts[2]);
		$deathDateJD = $startDate + $estamitedLife;   
		return jdtogregorian($deathDateJD);
	}
	  
	$birthPrint = date("F j, Y", strtotime($birth));	
	$estimated = 28307; // Rough total days in 77.5 years
	$today = date("m/d/Y");
	$death = deathDate($estimated, $birth);
	    
	$elapsed = dateDiff("/", $today, $birth);
	  $totalForGraph = round($totalDays / 30);
	$percentage = round(($elapsed / $estimated) * 100);
	
	$past = $elapsed;
	$future = dateDiff("/", $death, $today);
	
	// Divide by 60 to get a more reasonably-sized number
	$pastAdjusted = round($past / 60);
	$futureAdjusted = round($future / 60); 

	// Check if 78 or older
	if ($pastAdjusted >= 472) {
		$pastAdjusted = 470;
		$longLife = true;
	}
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title><?= $name ?>&#8217;s Lifeline</title>                           
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
	
	<link rel="apple-touch-icon" href="<?=$relativePath?>apple-touch-icon.png" />
	
	<!-- Import iPhone css -->
	<style type="text/css" media="only screen and (max-device-width: 480px)">
		@import "<?=$relativePath ?>css/iphone.css";
	</style>                      

	<!-- Import normal css -->
	<style type="text/css" media="screen and (min-device-width: 481px)">
		@import "<?=$relativePath ?>css/style.css";
	</style>
	    
	<!--[if IE]>
		<link href="<?=$relativePath ?>css/style.css" media="screen" rel="Stylesheet" type="text/css" />
  <![endif]-->
	
	<script type="text/javascript" charset="utf-8" src="<?=$relativePath ?>scripts/lifeline.js"></script>
</head>

<body>   
	<div id="supercontainer">
		<div id="container">
			<div id="name">
				<h1><?= $name ?></h1>
				<h2 class="birth">Born <?= $birthPrint ?></h2>
			</div>

			<div id="graph">
				<?php for ($p=1; $p <= $pastAdjusted; $p++) { ?>
					<div class="unit past"></div>
				<? } ?>
	
				<?php
					if (!$longLife) {
						for ($f=1; $f <= $futureAdjusted; $f++) { ?>
							<div class="unit future"></div>
				<? } } else { ?>
						  <div class="unitWide longLife"></div>
				<? } ?>
		 	</div>

			<div id="info">
				<dl>           
					<dt>Average American lifespan in years</dt>
					<dd><a href="http://en.wikipedia.org/wiki/Life_expectancy" title="Life expectancy - Wikipedia, the free encyclopedia">78</a></dd>
						
					<dt>Total estimated days</dt>
					<dd><?= $estimated ?></dd>    
		
					<dt>Days elapsed</dt>
					<dd><?= $elapsed ?></dd>
		
					<dt>Days elapsed as percentage of estimate</dt>
					<dd><?= $percentage ?></dd>  
				</dl>  
				
				<p class="cc">
					Lifeline is designed and developed by <a href="http://davidcorrell.net/" title="David Correll Design">David Correll.</a> Lifeline is licensed under the <a href="http://www.gnu.org/licenses/gpl.html" title="GNU GPL v3">GNU GPL v3 open source license.</a> Check out the project <a href="http://code.google.com/p/lifeline/" title="Lifeline at Google Code">here.</a>
				</p>
 			</div>
		</div>
	</div>
</body>
</html>
